package com.xiaoyu.lanling.media.image

import android.text.TextUtils
import android.view.View
import com.facebook.drawee.view.SimpleDraweeView
import com.facebook.imagepipeline.postprocessors.IterativeBoxBlurPostProcessor
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.media.image.UserImageLoadParam.Companion.newBuilder
import com.xiaoyu.lanling.view.UserAvatarDraweeView

object ImageViewUtil {

    fun loadImage(simpleDraweeView: SimpleDraweeView?, loadParam: ImageLoadParam?): Boolean {
        simpleDraweeView ?: return false

        if (loadParam == null) {
            simpleDraweeView.controller = null
            return false
        }
        if (simpleDraweeView.controller != null) {
            val key: String? = simpleDraweeView.getViewTag<String>(
                    Constant.GlobalViewTagDefine.VIEW_TAG_FOR_IMAGE_KEY
            )
            key?.let {
                if (it == loadParam.key) {
                    loadParam.setControllerFor(simpleDraweeView)
                    return false
                }
            }
        }
        loadParam.setControllerFor(simpleDraweeView)
        if (loadParam.placeHolderResId > 0) {
            simpleDraweeView.hierarchy?.setPlaceholderImage(loadParam.placeHolderResId)
        }
        if (loadParam.failImageResId > 0) {
            simpleDraweeView.hierarchy?.setFailureImage(loadParam.failImageResId)
        }
        simpleDraweeView.hierarchy?.setOverlayImage(
                if (loadParam.overlayResId <= 0) {
                    null
                } else AppContext.getDrawable(
                        loadParam.overlayResId
                )
        )
        simpleDraweeView.hierarchy?.roundingParams = loadParam.roundingParams
        simpleDraweeView.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_IMAGE_KEY, loadParam.key)

        if (simpleDraweeView is UserAvatarDraweeView) {
            val user = if (loadParam is UserImageLoadParam) loadParam.user else null
            simpleDraweeView.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER, user)
        }
        return true
    }

    fun loadImageOrGone(simpleDraweeView: SimpleDraweeView?, loadParam: ImageLoadParam): Boolean {
        return if (TextUtils.isEmpty(loadParam.key)) {
            simpleDraweeView?.visibility = View.GONE
            false
        } else {
            simpleDraweeView?.visibility = View.VISIBLE
            loadImage(simpleDraweeView, loadParam)
        }
    }

    fun loadImage(simpleDraweeView: SimpleDraweeView?, user: User, dp: Int, overlayResId: Int) {
        simpleDraweeView?.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER, user)
        val param = newBuilder().setUserAndDP(user, dp).setOverlayResId(overlayResId)?.build()
        loadImage(simpleDraweeView, param)
    }

    fun loadImage(simpleDraweeView: SimpleDraweeView?, user: User, dp: Int) {
        simpleDraweeView?.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER, user)
        val param = newBuilder().setUserAndDP(user, dp).build()
        loadImage(simpleDraweeView, param)
    }

    fun loadAvatarRound(simpleDraweeView: SimpleDraweeView?, user: User, dp: Int) {
        simpleDraweeView?.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER, user)
        val param = newBuilder().setUserAndDPRound(user, dp).build()
        loadImage(simpleDraweeView, param)
    }

    fun loadAvatarRoundWithBoard(simpleDraweeView: SimpleDraweeView?, user: User, dp: Int, borderWidthDP: Int, borderColorRes: Int, needDefaultAvatar: Boolean) {
        val param = newBuilder().setUserInfoAndDPRoundWithBorder(user, dp, borderWidthDP, borderColorRes, needDefaultAvatar).build()
        loadImage(simpleDraweeView, param)
        simpleDraweeView?.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER, user)
    }

    /**
     * 加载用户头像
     *
     * @param simpleDraweeView
     * @param user
     * @param dp
     * @param blurry 是否高斯模糊
     */
    fun loadImage(simpleDraweeView: SimpleDraweeView?, user: User, dp: Int, blurry: Boolean) {
        simpleDraweeView?.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER, user)
        // TODO (add blurry to ImageLoadParams)
        val builder = newBuilder().setUserAndDP(user, dp)
        if (blurry) {
            builder.setPostProcessor(IterativeBoxBlurPostProcessor(6, 12))
        }
        val param = builder.build()
        loadImage(simpleDraweeView, param)
    }

    fun loadImageWithUserParams(simpleDraweeView: SimpleDraweeView?, param: UserImageLoadParam) {
        simpleDraweeView?.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER, param.user)
        loadImage(simpleDraweeView, param)
    }

    fun loadImage(simpleDraweeView: SimpleDraweeView?, user: User, dp: Int, borderWidthDP: Int, borderColorRes: Int, needDefaultAvatar: Boolean) {
        simpleDraweeView?.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER, user)
        val param = newBuilder().setUserInfoAndDPWithBorder(
                user, dp, borderWidthDP, borderColorRes, needDefaultAvatar
        ).build()
        loadImage(simpleDraweeView, param)
    }

    fun loadImage(simpleDraweeView: SimpleDraweeView?, user: User, dp: Int, borderWidthDP: Int, borderColorRes: Int) {
        simpleDraweeView?.setViewTag(Constant.GlobalViewTagDefine.VIEW_TAG_FOR_USER, user)
        loadImage(simpleDraweeView, user, dp, borderWidthDP, borderColorRes, true)
    }
}